package com.zc.manager.device.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zc.core.metadata.unit.ValueUnits;
import com.zc.manager.device.domain.IotProduct;
import com.zc.manager.device.service.DeviceConfigMetadataManager;
import com.zc.manager.device.service.IIotProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品Controller
 *
 * @author xiepufeng
 * @date 2022-05-31
 */
@RestController
@RequestMapping("/iot/product")
public class IotProductController extends BaseController
{
    @Resource
    private IIotProductService iotProductService;

    @Resource
    private DeviceConfigMetadataManager configMetadataManager;

    /**
     * 分页查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('iot:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotProduct iotProduct)
    {
        startPage();
        List<IotProduct> list = iotProductService.selectIotProductList(iotProduct);
        return getDataTable(list);
    }
    /**
     * 无分页查询产品
     */
    @PreAuthorize("@ss.hasPermi('iot:product:list')")
    @GetMapping("/query")
    public AjaxResult query(IotProduct iotProduct)
    {
        List<IotProduct> list = iotProductService.selectIotProductList(iotProduct);
        return AjaxResult.success(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('iot:product:export')")
    @Log(title = "产品导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotProduct iotProduct)
    {
        List<IotProduct> list = iotProductService.selectIotProductList(iotProduct);
        ExcelUtil<IotProduct> util = new ExcelUtil<>(IotProduct.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(iotProductService.selectIotProductById(id));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('iot:product:add')")
    @Log(title = "产品新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody IotProduct iotProduct)
    {
        return toAjax(iotProductService.insertIotProduct(iotProduct));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('iot:product:edit')")
    @Log(title = "产品修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody IotProduct iotProduct)
    {
        return toAjax(iotProductService.updateIotProduct(iotProduct));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('iot:product:remove')")
    @Log(title = "产品删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(iotProductService.deleteIotProductByIds(ids));
    }

    /**
     * 发布产品
     */
    @PreAuthorize("@ss.hasPermi('iot:product:edit')")
    @Log(title = "产品发布", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/deploy/{id}")
    public AjaxResult deploy(@PathVariable("id") String id)
    {
        return toAjax(iotProductService.publishProduct(id));
    }

    /**
     * 取消发布产品
     */
    @PreAuthorize("@ss.hasPermi('iot:product:remove')")
    @Log(title = "取消发布", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/undeploy/{id}")
    public AjaxResult undeploy(@PathVariable("id") String id)
    {
        return toAjax(iotProductService.unPublishProduct(id));
    }

    /**
     * 更新产品认证配置
     *
     * @param id 产品id
     * @return 更新结果
     */
    @PreAuthorize("@ss.hasPermi('iot:product:edit')")
    @Log(title = "重置设备配置信息", businessType = BusinessType.UPDATE)
    @PutMapping("configuration/{id}")
    public AjaxResult updateConfiguration(@PathVariable String id, @RequestBody String configuration) {
        return AjaxResult.success(iotProductService.updateConfiguration(id, configuration));
    }

    /**
     * 获取单位数据
     * @return 单位数据
     */
    @GetMapping("/units")
    public AjaxResult allUnits() {
        return AjaxResult.success(ValueUnits.getAllUnit());
    }

    /**
     * 获取产品需要的配置定义信息
     * @return
     */
    @GetMapping("/config-metadata/{id}")
    public AjaxResult getConfigMetadata(@PathVariable("id") String id) {
        return AjaxResult.success(Mono.from(configMetadataManager.getProductConfigMetadata(id)).block());
    }

    /**
     * 更新协议支持
     */
    @PreAuthorize("@ss.hasPermi('iot:product:edit')")
    @Log(title = "协议支持", businessType = BusinessType.UPDATE)
    @PutMapping("/protocol-standby/{id}")
    public AjaxResult updateProtocolStandby(@PathVariable("id") String id, @RequestBody String protocolStandby)
    {
        return toAjax(iotProductService.updateProtocolStandby(id, protocolStandby));
    }

}
